from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pymysql
from pymysql import Error
import time
import random
from sc.ZDao import connect_mysql

# 内置User-Agent列表
USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"
]

city = {
    '北京': '530', '上海': '538', '广州': '763', '深圳': '765',
    '天津': '531', '武汉': '736', '西安': '854', '成都': '801',
    '沈阳': '599', '南京': '635', '杭州': '653', '苏州': '639',
    '重庆': '551', '长沙': '749', '厦门': '682', '南昌': '691'
}

def save_to_database(data, conn):
    """将数据保存到数据库"""
    try:
        with conn.cursor() as cursor:
            sql = """INSERT INTO jobs 
                     (jid, job_name, company_name, job_sal, job_loc, job_exp, job_edu) 
                     VALUES (NULL, %s, %s, %s, %s, %s, %s)"""
            cursor.execute(sql, (
                data['job_name'],
                data['company_name'],
                data['salary'],
                data['location'],
                data['experience'],
                data['education']
            ))
        conn.commit()
        return True
    except Error as e:
        print(f"❌ 数据保存失败: {e}")
        conn.rollback()
        return False

def run():
    area = input("请输入城市：")
    job = input("请输入岗位：")

    if area not in city:
        print("城市不支持")
        return

    # 连接数据库
    conn = connect_mysql()
    if not conn:
        print("无法继续，因为数据库连接失败")
        return

    city_id = city[area]
    url = f"https://sou.zhaopin.com/?jl={city_id}&kw={job}&p=1"

    # Edge 浏览器配置
    options = EdgeOptions()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument(f"user-agent={random.choice(USER_AGENTS)}")
    options.add_argument("--disable-blink-features=AutomationControlled")

    service = EdgeService(EdgeChromiumDriverManager().install())
    driver = webdriver.Edge(service=service, options=options)

    try:
        print(f"🔍 正在爬取: {job}岗位@{area}")
        driver.get(url)

        # 添加随机延迟
        time.sleep(random.uniform(3, 6))

        # 滚动页面以加载更多内容
        for _ in range(3):
            driver.execute_script("window.scrollBy(0, window.innerHeight)")
            time.sleep(random.uniform(1, 3))

        # 等待岗位元素加载
        try:
            WebDriverWait(driver, 15).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, ".joblist-box__item"))
            )
        except:
            print("⚠️ 页面加载超时或没有找到岗位元素")
            driver.quit()
            conn.close()
            return

        job_elements = driver.find_elements(By.CSS_SELECTOR, ".joblist-box__item")
        print(f"📊 共发现 {len(job_elements)} 个岗位")

        # 打印表头
        print("\n{:<5} {:<30} {:<25} {:<15} {:<10} {:<10} {:<10}".format(
            "序号", "职位名称", "公司名称", "薪资", "地点", "经验", "学历"
        ))
        print("-" * 100)

        saved_count = 0
        for i, job_el in enumerate(job_elements[:20], 1):  # 只处理前20个结果
            try:
                job_name = job_el.find_element(By.CSS_SELECTOR, ".jobinfo__name").text
                company_name = job_el.find_element(By.CSS_SELECTOR, ".companyinfo__name").text
                salary = job_el.find_element(By.CSS_SELECTOR, ".jobinfo__salary").text.strip()

                info_items = job_el.find_elements(By.CSS_SELECTOR, ".jobinfo__other-info-item")
                location = info_items[0].text.split("-")[0] if len(info_items) > 0 else area
                experience = info_items[1].text if len(info_items) > 1 else "不限"
                education = info_items[2].text if len(info_items) > 2 else "不限"

                print("{:<5} {:<30} {:<25} {:<15} {:<10} {:<10} {:<10}".format(
                    i,
                    job_name[:28] + "..." if len(job_name) > 28 else job_name,
                    company_name[:23] + "..." if len(company_name) > 23 else company_name,
                    salary,
                    location,
                    experience,
                    education
                ))

                job_data = {
                    'job_name': job_name,
                    'company_name': company_name,
                    'salary': salary,
                    'location': location,
                    'experience': experience,
                    'education': education
                }
                if save_to_database(job_data, conn):
                    saved_count += 1

            except Exception as e:
                print(f"⚠️ 处理第{i}个岗位时出错: {str(e)[:50]}...")

        print(f"\n✅ 爬取完成，共保存 {saved_count}/{len(job_elements)} 个岗位到数据库")

    except Exception as e:
        print(f"❌ 发生错误: {e}")
    finally:
        driver.quit()
        conn.close()

if __name__ == "__main__":
    run()
